/**
 * ipassbook 0.6.0
 * Copyright (C)2009 wateray and contributors
 * wateray@gmail.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package com.wateray.ipassbook.ui.model;

import java.util.Vector;

import javax.swing.AbstractListModel;
import javax.swing.ComboBoxModel;

import com.wateray.ipassbook.domain.Entity;

/**
 * @author wateray
 * @create 2009-5-23
 */
public class PassbookComboBoxModel extends AbstractListModel
		implements ComboBoxModel {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;
	private Vector<Entity> dataVector;
	
	private Entity selectedObject;

	/** Default nothing is selected. */
	private int selectedIndex = -1;

	
	public PassbookComboBoxModel(){
		dataVector = new Vector<Entity>();
	}
	
	@SuppressWarnings("unchecked")
	public PassbookComboBoxModel(Vector dataVector){
		setData(dataVector);
	}
	
	/**
	 * @param dataVector
	 * */
	@SuppressWarnings("unchecked")
	public void setData(Vector dataVector){
		this.dataVector = dataVector;
		selectedIndex = -1;
		selectedObject = null;
		fireContentsChanged(this, 0, getSize() -1 );
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.ListModel#getElementAt(int)
	 */
	@Override
	public  Object getElementAt(int index) {
		if (index >= 0 && index < dataVector.size()) {
			Entity entity = dataVector.elementAt(index);
			return entity.getEntityName();
		} else
			return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.ListModel#getSize()
	 */
	@Override
	public int getSize() {
		// TODO Auto-generated method stub
		return dataVector.size();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.ComboBoxModel#setSelectedItem(java.lang.Object)
	 */
	@Override
	public void setSelectedItem(Object anItem) {
		if ((selectedObject != null && !selectedObject.equals(anItem))
				|| selectedObject == null && anItem != null) {
			selectedIndex = getIndexOf(anItem);
			selectedObject = getObjectAt(selectedIndex);
			fireContentsChanged(this, -1, -1);
		}
	}

	@Override
	public Object getSelectedItem() {
		return getElementAt(selectedIndex);
	}

	/**
	 * @param anObject
	 * @return the object's index.
	 * */
	public int getIndexOf(Object anObject) {
		int index = -1;
		for (int i = 0; i < dataVector.size(); i++) {
			if (getElementAt(i).equals(anObject)) {
				index = i;
				break;
			}
		}
		return index;
	}

	/**
	 * @param index
	 * @return The object at index.
	 * */
	public Entity getObjectAt(int index) {
		if (index >= 0 && index < dataVector.size()) {
			return dataVector.elementAt(index);
		}
		return null;
	}
	
	/**
	 * @return The Entity Object as selected.
	 * */
	public Entity getSelectedObject() {
		return selectedObject;
	}
	
	/**
	 * @return Set selected Object by  Entity entityId.
	 * */
	public void setSelectedObjectById(String entityId) {
		for(int index =0; index < dataVector.size(); index++) {
			Entity entity = dataVector.elementAt(index);
			if(entity.getEntityId().equals(entityId)){
				setSelectedItem(getElementAt(index));
				break;
			}	
		}
	}
	
	/**
	 * @param object
	 * */
	public void removeItem(Object obj){
		int index = getIndexOf(obj);
		if(index != -1){
			dataVector.remove(index);
			fireIntervalRemoved(obj, index, index);
		}	
	}
	
}
